package com.ir;

import com.ast.NumberNode;
import com.sysdep.CodeGenerator;

public class BinExp extends Expr {
    Expr left;
    Expr right;
    String op;

    public BinExp(Expr left, Expr right, String op) {
        this.left = left;
        this.right = right;
        this.op = op;
    }

    public String getOp() {
        return op;
    }

    public Expr getLhs() {
        return left;
    }

    public Expr getRhs() {
        return right;
    }

    public String toString() {
        return "BinExp(" + left + " " + op + " " + right + ")" + "=" +getIntValue();
    }

    @Override
    public boolean accept(CodeGenerator cg) {
        return cg.visit(this);
    }

    @Override
    public int getIntValue() {
        switch (op) {
            case "+":
                return left.getIntValue() + right.getIntValue();
            case "-":
                return left.getIntValue() - right.getIntValue();

            case "*":
                return left.getIntValue() * right.getIntValue();

            case "/":
                return left.getIntValue() / right.getIntValue();

            case "%":
                return left.getIntValue() % right.getIntValue();
            case "<":
                return left.getIntValue() < right.getIntValue() ? 1 : 0;
            default:
                System.out.println("op is " + op);
                throw new Error("Unknown operator " + op);
        }
    }
}
